package org.dromara.third.convert.temporaryPowerWork.web;

import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.BusinessServiceException;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.ValidateUtil;
import org.dromara.system.domain.TemporaryPowerWork;
import org.dromara.system.mapper.TemporaryPowerWorkMapper;
import org.dromara.third.auth.beans.constans.ExceptionEnum;
import org.dromara.third.auth.common.auth.TokenThreadLocal;
import org.dromara.third.auth.util.AESUtil;
import org.dromara.third.convert.temporaryPowerWork.dto.TemporaryPowerWorkDTO;
import org.dromara.third.convert.temporaryPowerWork.validator.TemporaryPowerWorkValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@RestController
@Slf4j
@RequestMapping("/api/v1")
public class ConvertTemporaryPowerWorkController {

    @Autowired
    private TemporaryPowerWorkMapper temporaryPowerWorkMapper;

    @PostMapping("/temporaryPowerWork")
    @Transactional
    public R<Void> addTemporaryPowerWork(@RequestBody String jsonReq) {
        String orgCode = TokenThreadLocal.getThreadLocal();
        TemporaryPowerWorkDTO temporaryPowerWorkDTO;
        try {
            String decrypt = AESUtil.decrypt(jsonReq);
            temporaryPowerWorkDTO = JSONObject.parseObject(decrypt, TemporaryPowerWorkDTO.class);
        } catch (Exception e) {
            log.info("解析参数异常");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), ExceptionEnum.AuthException06.getMessage());
        }
        // 校验参数
        List<String> errorMessageList = new ArrayList<>();
        ValidateUtil.validate(errorMessageList, temporaryPowerWorkDTO, new TemporaryPowerWorkValidator());
        if (!CollectionUtils.isEmpty(errorMessageList)) {
            String errorMessage = StringUtils.join(errorMessageList, ",");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), errorMessage);
        }
        TemporaryPowerWork temporaryPowerWork = assembleTemporaryPowerWorkData(orgCode, temporaryPowerWorkDTO);
        int count = temporaryPowerWorkMapper.countWithOutDelFlag(temporaryPowerWork.getId());
        if (count == 0) {
            temporaryPowerWorkMapper.insert(temporaryPowerWork);
        } else {
            temporaryPowerWorkMapper.updateWithOutDelFlag(temporaryPowerWork);
        }
        return R.ok();
    }

    private TemporaryPowerWork assembleTemporaryPowerWorkData(String orgCode, TemporaryPowerWorkDTO dto) {
        TemporaryPowerWork temporaryPowerWork = new TemporaryPowerWork();
        try {
            temporaryPowerWork.setId(dto.getZyid());
            temporaryPowerWork.setZyzbh(dto.getZyzbh());
            temporaryPowerWork.setSqdw(dto.getSqdw());
            temporaryPowerWork.setZysqsj(DateUtils.parseDate(dto.getZysqsj()));
            temporaryPowerWork.setSqr(dto.getSqr());
            temporaryPowerWork.setZynr(dto.getZynr());
            temporaryPowerWork.setZywz(dto.getZywz());
            temporaryPowerWork.setJrdydgl(dto.getJrdydgl());
            temporaryPowerWork.setSbmedgl(dto.getSbmedgl());
            temporaryPowerWork.setGzdy(dto.getGzdy());
            temporaryPowerWork.setYdr(dto.getYdr());
            temporaryPowerWork.setZyryxx(dto.getZyryxx());
            temporaryPowerWork.setZydwlx(dto.getZydwlx());
            temporaryPowerWork.setZydwmc(dto.getZydwmc());
            temporaryPowerWork.setZyfzr(dto.getZyfzr());
            temporaryPowerWork.setZysskssj(DateUtils.parseDate(dto.getZysskssj()));
            temporaryPowerWork.setZyssjssj(DateUtils.parseDate(dto.getZyssjssj()));
            temporaryPowerWork.setSfsjqttszy(dto.getSfsjqttszy());
            temporaryPowerWork.setSjqttszyzbh(dto.getSjqttszyzbh());
            temporaryPowerWork.setFxbs(dto.getFxbs());
            temporaryPowerWork.setAqjdr(dto.getAqjdr());
            temporaryPowerWork.setJsjdr(dto.getJsjdr());
            temporaryPowerWork.setJhr(dto.getJhr());
            temporaryPowerWork.setDqzt(dto.getDqzt());
            temporaryPowerWork.setLongitude(dto.getLongitude());
            temporaryPowerWork.setLatitude(dto.getLatitude());
            temporaryPowerWork.setStartTime(DateUtils.parseDate(dto.getStartTime()));
            temporaryPowerWork.setEndTime(DateUtils.parseDate(dto.getEndTime()));
            temporaryPowerWork.setSpgbbm(dto.getSpgbbm());
            temporaryPowerWork.setFhyp(dto.getFhyp());
            temporaryPowerWork.setAttachment(dto.getAttachment());
            temporaryPowerWork.setDelFlag(Long.parseLong(dto.getDelFlag()));
            temporaryPowerWork.setCreateDept(Long.parseLong(dto.getSsbm()));
            temporaryPowerWork.setCreateBy(Long.parseLong(dto.getCreateBy()));
            temporaryPowerWork.setCreateTime(DateUtils.parseDate(dto.getCreateTime()));
            temporaryPowerWork.setUpdateBy(Long.parseLong(dto.getUpdateBy()));
            temporaryPowerWork.setUpdateTime(DateUtils.parseDate(dto.getUpdateTime()));
            temporaryPowerWork.setTenantId(orgCode);
        } catch (Exception e) {
            throw new BusinessServiceException(ExceptionEnum.AuthException07.getCode(), "组装临时用电作业信息异常");
        }
        return temporaryPowerWork;
    }
}
